package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.ApplyForPostpayCall;
import com.ymatou.iapi.sellertrading.service.CancelPostpayCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtOrders;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersIMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_CancelPostpay {
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单撤销补款");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new CancelPostpayCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelPostpay_001() {
		Logger.start(true, "卖家撤销补款成功");
		try {
			 Logger.comment("前置数据准备");
	         int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			 List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(sellerId);
			 int orderId = list.get(0).getIorderid();
		        
		     BigDecimal PostPayAmount = new BigDecimal(40);
		     // 调用申请补款
		     new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
		     Logger.comment("前置数据准备结束");
		        
		     JSONObject aaJsonObject = new CancelPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
	        
		     // 数据库验证
		     DefaultWapper database = new DefaultWapper();
		     HashMap<String, Integer> m = new HashMap<String, Integer>();
		     m.put("OrderId", orderId);
		     m.put("TradingStatus", 17);
		     m.put("TraddingId", orderId);
		     m.put("SellerId", sellerId);
		     Thread.sleep(500);
		     Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
		     Logger.verifyEquals(-1, database.selectOrderPostPayByOrderId(orderId).get(0).get("iAction"), "验证Action状态");
			 Logger.verifyEquals(2, database.selectTradingStatus(m).size(), "验证交易表记录");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelPostpay_002() {
		Logger.start(true, "订单不存在");
		try {
			 Logger.comment("前置数据准备");
	         int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
	         int orderId = 1212131;
	         Logger.comment("前置数据准备结束");
	         // 调用撤销补款
	         JSONObject aaJsonObject = new CancelPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
	         Logger.verifyEquals(201, aaJsonObject.getInt("Code"), "验证Code");
	         JSONObject aa=  (JSONObject) aaJsonObject.getJSONArray("Data").get(0);
	         Logger.verifyIsContains(true, aa.getString("Reason").contains("订单不存在:"), "验证msg");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	 @Test
	  @Category(P1.class)
	@TestCase
	public void Tc_CancelPostpay_003() {
		Logger.start(true, "当前状态不能撤销补款申请");
		try {
			Logger.comment("前置数据准备");
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbyStatus(4);
			int sellerId = list.get(0).getIbuyerid();
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
	        JSONObject   aaJsonObject = new CancelPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
			Logger.verifyEquals(201,  aaJsonObject.getInt("Code"), "验证Code");
			JSONObject aa=  (JSONObject) aaJsonObject.getJSONArray("Data").get(0);
            Logger.verifyEquals(true, aa.getString("Reason").contains("当前状态不能取消补款申请"), "验证Msg");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
   
	 @Test
     @Category(P1.class)
     @TestCase
    public void Tc_CancelPostpay_004() {
        Logger.start(true, "无对应的补款记录，撤销失败");
        try {
        	Logger.comment("前置数据准备");
	         int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			 List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(sellerId);
			 int orderId = list.get(0).getIorderid();
		     BigDecimal PostPayAmount = new BigDecimal(40);
		     // 调用申请补款
		     new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
		     // 删除补款记录
		     new DefaultWapper().deleteOrderPostPayByOrderId(orderId);
		     Logger.comment("前置数据准备结束");
		     JSONObject   aaJsonObject = new CancelPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
		     Logger.verifyEquals(201,  aaJsonObject.getInt("Code"), "验证Code");
		     JSONObject aa=  (JSONObject) aaJsonObject.getJSONArray("Data").get(0);
	            Logger.verifyEquals(true, aa.getString("Reason").contains("当前订单无对应的补款记录"), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

}
